﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web.Script.Serialization;

namespace M.Dao.Client.Core
{
    public class DataAccess : HttpServiceBase,IDataAccess 
    {   
        private string _url = string.Empty;

        public string Url
        {
            get
            {
                return _url;
            }
            set
            {   
                _url = value;
            }
        }
        
        public DataAccess(string daoUrl)
        {
            this.Url = daoUrl;
        }


        public QueryResult Query(string db, string accessToken, string sql, string pk, int pageSize, int pageNo)
        {
            string queryUrl = Path.Combine(this.Url, string.Format("odata/query/{0}/{1}/{2}", pageNo, pageSize, pk));
            
            KeyValuePair<string,string> kvDB = new KeyValuePair<string,string>("DB",db);
            KeyValuePair<string, string> kvAT = new KeyValuePair<string, string>("AT", accessToken);

            string httpResult = this.Post(queryUrl, sql, kvDB,kvAT);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QueryResult>(httpResult);

            return result;
        }
        public QueryResult<T> Query<T>(string db, string accessToken, string sql, string pk, int pageSize, int pageNo)
        {
            string queryUrl = Path.Combine(this.Url, string.Format("odata/query/{0}/{1}/{2}", pageNo, pageSize, pk));

            KeyValuePair<string, string> kvDB = new KeyValuePair<string, string>("DB", db);
            KeyValuePair<string, string> kvAT = new KeyValuePair<string, string>("AT", accessToken);

            string httpResult = this.Post(queryUrl, sql, kvDB,kvAT);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QueryResult<T>>(httpResult);

            return result;
        }
        public GetResult Get(string db, string accessToken, string sql)
        {
            string getUrl = Path.Combine(this.Url, "/odata/get");
            KeyValuePair<string, string> kvDB = new KeyValuePair<string, string>("DB", db);
            KeyValuePair<string, string> kvAT = new KeyValuePair<string, string>("AT", accessToken);

            string httpResult = this.Post(getUrl, sql, kvDB,kvAT);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<GetResult>(httpResult);

            return result;
        }

        public WriteResult Write(string db, string accessToken, string sql)
        {
            string writeUrl = Path.Combine(this.Url, "/odata/write");
            KeyValuePair<string, string> kvDB = new KeyValuePair<string, string>("DB", db);

            string httpResult = this.Post(writeUrl, sql, kvDB);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<WriteResult>(httpResult);

            return result;
        }

        public ScaleResult Scale(string db, string accessToken, string sql)
        {
            string writeUrl = Path.Combine(this.Url, "/odata/scale");
            KeyValuePair<string, string> kvDB = new KeyValuePair<string, string>("DB", db);
            KeyValuePair<string, string> kvAT = new KeyValuePair<string, string>("AT", accessToken);

            string httpResult = this.Post(writeUrl, sql, kvDB, kvAT);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ScaleResult>(httpResult);

            return result;
        }

        public DataResult Test(string db, string accessToken)
        {
            string writeUrl = Path.Combine(this.Url, "/odata/test");
            KeyValuePair<string, string> kvDB = new KeyValuePair<string, string>("DB", db);
            KeyValuePair<string, string> kvAT = new KeyValuePair<string, string>("AT", accessToken);

            string httpResult = this.Post(writeUrl, string.Empty, kvDB, kvAT);

            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<DataResult>(httpResult);

            return result;
        }

        
    }
}
